<?php
    App::uses('Component', 'Controller');   
    App::uses('ViewAppController', 'View.Controller');     
    App::import('helper', 'String');
    App::import('Helper', 'String');
    App::uses('View', 'View');                
    require('PHPMailerAutoload.php');
    class ProcessComponent extends Component {
        function siteURL()
        {            
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            $domainName = $_SERVER['HTTP_HOST'].'/';                   
            return $protocol.$domainName;
        } 
        function VerifyMailAddress($address) 
        {
            $valid = preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$address);
            if(!$valid){
                return false;
            }else{
                return true;
            }
        }             
        public function sendMail($subject='[USIS]',$content="Pegasus",$mail_address){
            $siteURL = self::siteURL();
            $status = false;  
            $mail = new PHPMailer();
            $mail->FromName = 'U.S. Investment Services <no-reply@usis.us>'; 
            $mail->IsSMTP(true);
            $mail->SMTPSecure = "ssl"; 
            $mail->Port = "465"; 
            $mail->Host     = 'smtp.gmail.com'; 
            //$mail->SMTPDebug  = 2;
            $mail->Mailer   = 'smtp';
            $mail->SMTPAuth = 'true'; // turn on SMTP authentication
            $mail->sendAs = 'html';
            $mail->Username = 'tunv@greenglobal.vn';// SMTP username
            $mail->Password = 'tunv123-'; // SMTP password            
            //$mail->AddReplyTo($config['AddReplyTo'],$config['SubTitle']);
            $mail->addAddress($mail_address);
            //$mail->addCC('tunguyenvan.it@gmail.com');
            $mail->AltBody = 'This is a plain-text message body';
            //Set the subject line
            $mail->Subject = $subject;
            $message = '';
            $mail->MsgHTML(true);
            $mail->Body    = $content;
            $mail->CharSet = 'utf-8';
            //send the message, check for errors
            if ($mail->send()) {
                $status = true;
            }
            return $status;
        }
        public function call_helpers(){
            $this->View = new View($this->Controller);    
            $hlp = new StringHelper($this->View);
            return $hlp;
        }
        public function nodeFields(){
            return array('Node.id','Node.title','Node.alias','Node.field_description','Node.field_body','Node.created','Node.updated','Node.field_image_default','Node.field_priority','Node.field_link_page'); 
        }
        public function orderFields(){
            return array('COALESCE(Node.field_priority,999999999)' => 'ASC','Node.updated' => 'desc','Node.created' => 'desc'); 
        }    
        public function condsDefault(){
            return array('Node.is_published' => 1); 
        }
    }    
?>
